package com.zhang.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zhang.entity.PlatformPropertyKey;
import com.zhang.entity.PlatformPropertyValue;
import com.zhang.mapper.PlatformPropertyKeyMapper;
import com.zhang.service.PlatformPropertyKeyService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhang.service.PlatformPropertyValueService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * <p>
 * 属性表 服务实现类
 * </p>
 *
 * @author zbw
 * @since 2022-04-03
 */
@Service
public class PlatformPropertyKeyServiceImpl extends ServiceImpl<PlatformPropertyKeyMapper, PlatformPropertyKey> implements PlatformPropertyKeyService {
    @Autowired
    private PlatformPropertyValueService platformPropertyValueService;
    //根据商品分类id查询平台属性
    //第一种写法，性能不高
    /*@Override
    public List<PlatformPropertyKey> getPlatformPropertyByCategoryId(Long category1Id, Long category2Id, Long category3Id) {
        //1、根据商品分类id查询该id的平台属性名称
        List<PlatformPropertyKey> platformPropertyKeyList=baseMapper.getPlatformPropertyByCategoryId(category1Id,category2Id,category3Id);
        *//*
        循环查数据库，如果数据库字段过多，该方式性能太低
         *//*
        //2、根据平台属性名称的id查询平台属性值集合
        for (PlatformPropertyKey platformPropertyKey : platformPropertyKeyList) {
            QueryWrapper<PlatformPropertyValue> wrapper = new QueryWrapper<>();
            wrapper.eq("property_key_id",platformPropertyKey.getPropertyKey());
            List<PlatformPropertyValue> propertyValueList=platformPropertyValueService.list(wrapper);
            platformPropertyKey.setPropertyValueList(propertyValueList);
        }
        return platformPropertyKeyList;
    }*/

    //根据商品分类id查询平台属性
    //第二种写法，只用一条SQL语句就可以实现
    @Override
    public List<PlatformPropertyKey> getPlatformPropertyByCategoryId(Long category1Id, Long category2Id, Long category3Id) {

        return baseMapper.getPlatformPropertyByCategoryId2(category1Id,category2Id,category3Id);
    }

    //保存平台属性
    @Transactional
    @Override
    public void savePlatformProperty(PlatformPropertyKey platformPropertyKey) {
        //修改平台属性
        if (platformPropertyKey.getId()!=null){
            baseMapper.updateById(platformPropertyKey);
            QueryWrapper<PlatformPropertyValue> wrapper = new QueryWrapper<>();
            wrapper.eq("property_key_id",platformPropertyKey.getId());
            platformPropertyValueService.remove(wrapper);
        }else {
            //1、保存平台属性的key
            baseMapper.insert(platformPropertyKey);
        }
        //2、保存平台属性value
        List<PlatformPropertyValue> propertyValueList = platformPropertyKey.getPropertyValueList();
        //判断平台属性值list是否为空
        if (!CollectionUtils.isEmpty(propertyValueList)){
            for (PlatformPropertyValue platformPropertyValue : propertyValueList) {
                platformPropertyValue.setPropertyKeyId(platformPropertyKey.getId());
            }
            platformPropertyValueService.saveBatch(propertyValueList);
        }
    }

    //4.根据skuId查询商品平台属性
    @Override
    public List<PlatformPropertyKey> getPlatformPropertyBySkuId(Long skuId) {
        return baseMapper.getPlatformPropertyBySkuId(skuId);
    }
}
